#include "resources.h"
#include "map.h"
#include <stdio.h>

static BITMAP* safe_load_bitmap(const char* use, const char* filename)
{
	BITMAP* bmp = load_bitmap(filename, NULL);
	if (bmp == NULL) {
		fprintf(stderr, "ERROR: %s Bitmap (%s) didn't load!", use, filename);
	}
	return bmp;
}

static void safe_destroy_bitmap(BITMAP *bmp)
{
	if (bmp != NULL) {
		destroy_bitmap(bmp);
	}
}

static FONT* safe_load_font(const char* use, const char* filename)
{
	FONT* font = load_font(filename, NULL, NULL);
	if (font == NULL) {
		fprintf(stderr, "ERROR: %s Font (%s) didn't load!", use, filename);
	}
	return font;
}

static void safe_destroy_font(FONT* font)
{
	if (font != NULL) {
		destroy_font(font);
	}
}

/**
 * Vorbedingung: Allegro ist initialisiert
 */
void resources_load(void)
{
	bmp.tileset = safe_load_bitmap("Tileset", "gfx/tileset.tga");
	bmp.objects = safe_load_bitmap("Objects", "gfx/objects.tga");
	bmp.icons = safe_load_bitmap("Icons", "gfx/items.tga");
	for (int i = 1; i < WEAPONS_RANGE_END; i++) {
		//bmp.weapons[i] = safe_load_bitmap("Weapon", items[i].file);
	}
	bmp.player = safe_load_bitmap("Player", "gfx/p1_sprites.tga");
	fnt.sans = safe_load_font("Sans", "sans.tga");
	
	make_trans_font(fnt.sans);
}

/**
 * Vorbedingung: resources_load wurde erfolgreich ausgeführt
 */
void resources_unload(void)
{
	safe_destroy_bitmap(bmp.tileset);
	safe_destroy_bitmap(bmp.objects);
	safe_destroy_bitmap(bmp.icons);
	for (int i = 1; i < WEAPONS_RANGE_END; i++) {
		//safe_destroy_bitmap(bmp.weapons[i]);
	}
	safe_destroy_bitmap(bmp.player);
	safe_destroy_font(fnt.sans);
}

